/*
 * @Date: 2021-12-12 19:47:04
 * @LastEditors: LeeKing
 * @LastEditTime: 2021-12-16 14:30:53
 * @FilePath: \admin\src\store\modules\user.js
 */
import { setToken, getToken, removeToken } from '@/utils/auth'
import { login } from '@/api/user'

const state = {
  token: getToken(),
  userInfo: {}
}
const mutations = {
  setToken(state, payload) {
    state.token = payload
    setToken(payload)
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  logOut(state) {
    state.token = null
    state.userInfo = {}
    removeToken()
  }
}
const actions = {
  async login(context, payload) {
    const res = await login(payload)
    context.commit('setToken', res.data.token)
    context.commit('setUserInfo', res.data)
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
